/*=====================================================================*
 *                   Copyright (C) 2011 Paul Mineiro                   *
 * All rights reserved.                                                *
 *                                                                     *
 * Redistribution and use in source and binary forms, with             *
 * or without modification, are permitted provided that the            *
 * following conditions are met:                                       *
 *                                                                     *
 *     * Redistributions of source code must retain the                *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer.                                       *
 *                                                                     *
 *     * Redistributions in binary form must reproduce the             *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer in the documentation and/or            *
 *     other materials provided with the distribution.                 *
 *                                                                     *
 *     * Neither the name of Paul Mineiro nor the names                *
 *     of other contributors may be used to endorse or promote         *
 *     products derived from this software without specific            *
 *     prior written permission.                                       *
 *                                                                     *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND              *
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,         *
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES               *
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE             *
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER               *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,                 *
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES            *
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE           *
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR                *
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF          *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY              *
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE             *
 * POSSIBILITY OF SUCH DAMAGE.                                         *
 *                                                                     *
 * Contact: Paul Mineiro <paul@mineiro.com>                            *
 *=====================================================================*/

#ifndef __PIANOVEC_HH__
#define __PIANOVEC_HH__

#include <vector>

namespace playerpiano
{

  template<typename Expr,
           typename Guard>
  class SVMult;

  template<typename Expr>
  class SVMult<Expr, bool>
    {
      private:
        float a;
        const Expr& b;

      public:
        typedef bool is_pianovec_expr;
        explicit SVMult (float _a, const Expr& _b) : a (_a), b (_b) { }
        float operator[] (unsigned int i) const { return a * b[i]; }
        size_t size () const { return b.size (); }
    };

  template<typename ExprA,
           typename ExprB,
           typename GuardA,
           typename GuardB>
  class VVAdd;

  template<typename ExprA,
           typename ExprB>
  class VVAdd<ExprA, ExprB, bool, bool>
    {
      private:
        const ExprA& a;
        const ExprB& b;

      public:
        typedef bool is_pianovec_expr;
        explicit VVAdd (const ExprA& _a, const ExprB& _b) : a (_a), b (_b) { } 
        float operator[] (unsigned int i) const { return a[i] + b[i]; }
        size_t size () const { return b.size (); }
    };

  class PianoVec
    {
      private:
        std::vector<float> x;

      public:
        typedef bool is_pianovec_expr;
        explicit PianoVec () : x () { }
        explicit PianoVec (size_t s) : x (s) { }
        explicit PianoVec (size_t s, float  init) : x (s, init) { }
        explicit PianoVec (const std::vector<float>& _x) : x (_x) { }

        template<typename Expr>
        PianoVec&
        operator+= (const Expr& e)
          {
            for (unsigned int i = 0; i < x.size (); ++i)
              {
                x[i] += e[i];
              }

            return *this;
          }

        float operator[] (unsigned int i) const { return x[i]; }
        float& operator[] (unsigned int i) { return x[i]; }
        size_t size () const { return x.size (); }
    };

  template<typename Expr>
  SVMult<Expr, typename Expr::is_pianovec_expr>
  operator* (float       a,
             const Expr& b)
    {
      return SVMult<Expr, typename Expr::is_pianovec_expr> (a, b);
    }

  template<typename ExprA,
           typename ExprB>
  VVAdd<ExprA, 
        ExprB,
        typename ExprA::is_pianovec_expr,
        typename ExprB::is_pianovec_expr>
  operator+ (const ExprA& a,
             const ExprB& b)
    {
      return VVAdd<ExprA, 
                   ExprB,
                   typename ExprA::is_pianovec_expr,
                   typename ExprB::is_pianovec_expr> (a, b);
    }

  template<typename ExprAA,
           typename ExprAB,
           typename ExprBA,
           typename ExprBB>
  float
  operator* (const VVAdd<ExprAA, ExprAB, bool, bool>& a,
             const VVAdd<ExprBA, ExprBB, bool, bool>& b)
    {
      float rv = 0;

      for (unsigned int i = 0; i < a.size (); ++i)
        {
          rv += a[i] * b[i];
        }

      return rv;
    }
}

#endif // __PIANOVEC_HH__
